﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace iKnow.Extensions
{
    public static class LinqExtensions
    {
        public static T WithMax<T, TValue>(this IEnumerable<T> source, Func<T, TValue> selector)
        {
            var max = default(TValue);
            var withMax = default(T);
            bool first = true;
            var comparer = Comparer<TValue>.Default;
            foreach (var item in source)
            {
                var value = selector(item);
                int compare = comparer.Compare(value, max);

                if (compare > 0 || first)
                {
                    max = value;
                    withMax = item;
                }
                first = false;
            }
            return withMax;
        }
    }
}